home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / mfb / mfbbitblt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-28  |  28.0 KB  |  1,105 lines

  1. /* Combined Purdue/PurduePlus patches, level 2.0, 1/17/89 */
  2. /***********************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25. /* $XConsortium: mfbbitblt.c,v 5.14 90/02/08 13:29:24 rws Exp $ */
  26. #include "X.h"
  27. #include "Xprotostr.h"
  28.  
  29. #include "miscstruct.h"
  30. #include "regionstr.h"
  31. #include "gcstruct.h"
  32. #include "windowstr.h"
  33. #include "pixmapstr.h"
  34. #include "scrnintstr.h"
  35.  
  36. #include "mi.h"
  37.  
  38. #include "mfb.h"
  39. #include "maskbits.h"
  40.  
  41.  
  42. /* CopyArea and CopyPlane for a monchrome frame buffer
  43.  
  44.  
  45.     clip the source rectangle to the source's available bits.  (this
  46. avoids copying unnecessary pieces that will just get exposed anyway.)
  47. this becomes the new shape of the destination.
  48.     clip the destination region to the composite clip in the
  49. GC.  this requires translating the destination region to (dstx, dsty).
  50.     build a list of source points, one for each rectangle in the
  51. destination.  this is a simple translation.
  52.     go do the multiple rectangle copies
  53.     do graphics exposures
  54. */
  55. /** Optimized for drawing pixmaps into windows, especially when drawing into
  56.  ** unobscured windows.  Calls to the general-purpose region code were
  57.  ** replaced with rectangle-to-rectangle clipping comparisions.  This is
  58.  ** possible, since the pixmap is a single rectangle.  In an unobscured
  59.  ** window, the destination clip is also a single rectangle, and region
  60.  ** code can be avoided entirely.  This is a big savings, since the region
  61.  ** code uses XAlloc() and makes many function calls.
  62.  **
  63.  ** In addition, if source is a pixmap, there is no need to call the
  64.  ** expensive miHandleExposures() routine.  Instead, we simply return NULL.
  65.  **
  66.  ** Previously, drawing a pixmap into an unobscured window executed at least
  67.  ** 8 XAlloc()'s, 30 function calls, and hundreds of lines of code.
  68.  **
  69.  ** Now, the same operation requires no XAlloc()'s, no region function calls,
  70.  ** and much less overhead.  Nice for drawing lots of small pixmaps.
  71.  */
  72.  
  73. RegionPtr
  74. mfbCopyArea(pSrcDrawable, pDstDrawable,
  75.         pGC, srcx, srcy, width, height, dstx, dsty)
  76. register DrawablePtr pSrcDrawable;
  77. register DrawablePtr pDstDrawable;
  78. register GC *pGC;
  79. int srcx, srcy;
  80. int width, height;
  81. int dstx, dsty;
  82. {
  83.     RegionPtr prgnSrcClip;    /* may be a new region, or just a copy */
  84.     Bool freeSrcClip = FALSE;
  85.  
  86.     RegionPtr prgnExposed;
  87.     RegionRec rgnDst;
  88.     DDXPointPtr pptSrc;
  89.     register DDXPointPtr ppt;
  90.     register BoxPtr pbox;
  91.     int i;
  92.     register int dx;
  93.     register int dy;
  94.     xRectangle origSource;
  95.     DDXPointRec origDest;
  96.     int numRects;
  97.     BoxRec fastBox;
  98.     int fastClip = 0;        /* for fast clipping with pixmap source */
  99.     int fastExpose = 0;        /* for fast exposures with pixmap source */
  100.  
  101.     origSource.x = srcx;
  102.     origSource.y = srcy;
  103.     origSource.width = width;
  104.     origSource.height = height;
  105.     origDest.x = dstx;
  106.     origDest.y = dsty;
  107.  
  108.     if ((pSrcDrawable != pDstDrawable) &&
  109.     pSrcDrawable->pScreen->SourceValidate)
  110.     (*pSrcDrawable->pScreen->SourceValidate) (pSrcDrawable, srcx, srcy, width, height);
  111.  
  112.     srcx += pSrcDrawable->x;
  113.     srcy += pSrcDrawable->y;
  114.  
  115.     /* clip the source */
  116.  
  117.     if (pSrcDrawable->type == DRAWABLE_PIXMAP)
  118.     {
  119.     if ((pSrcDrawable == pDstDrawable) &&
  120.         (pGC->clientClipType == CT_NONE))
  121.     {
  122.         prgnSrcClip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  123.     }
  124.     else
  125.     {
  126.         /* Pixmap sources generate simple exposure events */
  127.         fastExpose = 1;
  128.  
  129.         /* Pixmap is just one clipping rectangle so we can avoid
  130.            allocating a full-blown region. */
  131.         fastClip = 1;
  132.  
  133.         fastBox.x1 = srcx;
  134.         fastBox.y1 = srcy;
  135.         fastBox.x2 = srcx + width;
  136.         fastBox.y2 = srcy + height;
  137.         
  138.         /* Left and top are already clipped, so clip right and bottom */
  139.         if (fastBox.x2 > pSrcDrawable->x + (int) pSrcDrawable->width)
  140.           fastBox.x2 = pSrcDrawable->x + (int) pSrcDrawable->width;
  141.         if (fastBox.y2 > pSrcDrawable->y + (int) pSrcDrawable->height)
  142.           fastBox.y2 = pSrcDrawable->y + (int) pSrcDrawable->height;
  143.     }
  144.     }
  145.     else
  146.     {
  147.     if (pGC->subWindowMode == IncludeInferiors)
  148.     {
  149.         if ((pSrcDrawable == pDstDrawable) &&
  150.         (pGC->clientClipType == CT_NONE))
  151.         {
  152.         prgnSrcClip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  153.         }
  154.         else
  155.         {
  156.         prgnSrcClip = NotClippedByChildren((WindowPtr)pSrcDrawable);
  157.         freeSrcClip = TRUE;
  158.         }
  159.     }
  160.     else
  161.     {
  162.         prgnSrcClip = &((WindowPtr)pSrcDrawable)->clipList;
  163.     }
  164.     }
  165.  
  166.     /* Don't create a source region if we are doing a fast clip */
  167.     if (!fastClip)
  168.     {
  169.     BoxRec srcBox;
  170.  
  171.     srcBox.x1 = srcx;
  172.     srcBox.y1 = srcy;
  173.     srcBox.x2 = srcx + width;
  174.     srcBox.y2 = srcy + height;
  175.     
  176.     (*pGC->pScreen->RegionInit)(&rgnDst, &srcBox, 1);
  177.     (*pGC->pScreen->Intersect)(&rgnDst, &rgnDst, prgnSrcClip);
  178.     }
  179.     
  180.     dstx += pDstDrawable->x;
  181.     dsty += pDstDrawable->y;
  182.  
  183.     if (pDstDrawable->type == DRAWABLE_WINDOW)
  184.     {
  185.     if (!((WindowPtr)pDstDrawable)->realized)
  186.     {
  187.         if (!fastClip)
  188.         (*pGC->pScreen->RegionUninit)(&rgnDst);
  189.         if (freeSrcClip)
  190.         (*pGC->pScreen->RegionDestroy)(prgnSrcClip);
  191.         return NULL;
  192.     }
  193.     }
  194.  
  195.     dx = srcx - dstx;
  196.     dy = srcy - dsty;
  197.  
  198.     /* Translate and clip the dst to the destination composite clip */
  199.     if (fastClip)
  200.     {
  201.     RegionPtr cclip;
  202.  
  203.         /* Translate the region directly */
  204.         fastBox.x1 -= dx;
  205.         fastBox.x2 -= dx;
  206.         fastBox.y1 -= dy;
  207.         fastBox.y2 -= dy;
  208.  
  209.     /* If the destination composite clip is one rectangle we can
  210.        do the clip directly.  Otherwise we have to create a full
  211.        blown region and call intersect */
  212.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  213.         if (REGION_NUM_RECTS(cclip) == 1)
  214.         {
  215.         BoxPtr pBox = REGION_RECTS(cclip);
  216.       
  217.         if (fastBox.x1 < pBox->x1) fastBox.x1 = pBox->x1;
  218.         if (fastBox.x2 > pBox->x2) fastBox.x2 = pBox->x2;
  219.         if (fastBox.y1 < pBox->y1) fastBox.y1 = pBox->y1;
  220.         if (fastBox.y2 > pBox->y2) fastBox.y2 = pBox->y2;
  221.  
  222.         /* Check to see if the region is empty */
  223.         if (fastBox.x1 >= fastBox.x2 || fastBox.y1 >= fastBox.y2)
  224.         (*pGC->pScreen->RegionInit)(&rgnDst, NullBox, 0);
  225.         else
  226.         (*pGC->pScreen->RegionInit)(&rgnDst, &fastBox, 1);
  227.     }
  228.         else
  229.     {
  230.         /* We must turn off fastClip now, since we must create
  231.            a full blown region.  It is intersected with the
  232.            composite clip below. */
  233.         fastClip = 0;
  234.         (*pGC->pScreen->RegionInit)(&rgnDst, &fastBox,1);
  235.     }
  236.     }
  237.     else
  238.     {
  239.         (*pGC->pScreen->TranslateRegion)(&rgnDst, -dx, -dy);
  240.     }
  241.  
  242.     if (!fastClip)
  243.     {
  244.     (*pGC->pScreen->Intersect)(&rgnDst,
  245.                    &rgnDst,
  246.                  ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip);
  247.     }
  248.  
  249.     /* Do bit blitting */
  250.     numRects = REGION_NUM_RECTS(&rgnDst);
  251.     if (numRects)
  252.     {
  253.     if(!(pptSrc = (DDXPointPtr)ALLOCATE_LOCAL(numRects *
  254.                           sizeof(DDXPointRec))))
  255.     {
  256.         (*pGC->pScreen->RegionUninit)(&rgnDst);
  257.         if (freeSrcClip)
  258.         (*pGC->pScreen->RegionDestroy)(prgnSrcClip);
  259.         return NULL;
  260.     }
  261.     pbox = REGION_RECTS(&rgnDst);
  262.     ppt = pptSrc;
  263.     for (i = numRects; --i >= 0; pbox++, ppt++)
  264.     {
  265.         ppt->x = pbox->x1 + dx;
  266.         ppt->y = pbox->y1 + dy;
  267.     }
  268.     
  269.     if (pGC->planemask & 1)
  270.         mfbDoBitblt(pSrcDrawable, pDstDrawable, pGC->alu, &rgnDst, pptSrc);
  271.     DEALLOCATE_LOCAL(pptSrc);
  272.     }
  273.  
  274.     prgnExposed = NULL;
  275.     if (((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->fExpose) {
  276.         /* Pixmap sources generate a NoExposed (we return NULL to do this) */
  277.         if (!fastExpose)
  278.         prgnExposed =
  279.         miHandleExposures(pSrcDrawable, pDstDrawable, pGC,
  280.                   origSource.x, origSource.y,
  281.                   (int)origSource.width,
  282.                   (int)origSource.height,
  283.                   origDest.x, origDest.y, (unsigned long)0);
  284.     }
  285.     (*pGC->pScreen->RegionUninit)(&rgnDst);
  286.     if (freeSrcClip)
  287.     (*pGC->pScreen->RegionDestroy)(prgnSrcClip);
  288.     return prgnExposed;
  289. }
  290.  
  291. /* DoBitblt() does multiple rectangle moves into the rectangles
  292.    DISCLAIMER:
  293.    this code can be made much faster; this implementation is
  294. designed to be independent of byte/bit order, processor
  295. instruction set, and the like.  it could probably be done
  296. in a similarly device independent way using mask tables instead
  297. of the getbits/putbits macros.  the narrow case (w<32) can be
  298. subdivided into a case that crosses word boundaries and one that
  299. doesn't.
  300.  
  301.    we have to cope with the dircetion on a per band basis,
  302. rather than a per rectangle basis.  moving bottom to top
  303. means we have to invert the order of the bands; moving right
  304. to left requires reversing the order of the rectangles in
  305. each band.
  306.  
  307.    if src or dst is a window, the points have already been
  308. translated.
  309. */
  310.  
  311. #ifdef FASTGETBITS
  312. #define getunalignedword(psrc, x, dst) { \
  313.     register int _tmp; \
  314.     FASTGETBITS(psrc, x, 32, _tmp); \
  315.     dst = _tmp; \
  316. }
  317. #else
  318. #define getunalignedword(psrc, x, dst) \
  319. { \
  320.     dst = (SCRLEFT((unsigned) *(psrc), (x))) | \
  321.       (SCRRIGHT((unsigned) *((psrc)+1), 32-(x))); \
  322. }
  323. #endif  /* FASTGETBITS */
  324.  
  325. #include "fastblt.h"
  326.  
  327. mfbDoBitblt(pSrcDrawable, pDstDrawable, alu, prgnDst, pptSrc)
  328. DrawablePtr pSrcDrawable;
  329. DrawablePtr pDstDrawable;
  330. unsigned char alu;
  331. RegionPtr prgnDst;
  332. DDXPointPtr pptSrc;
  333. {
  334.     unsigned int *psrcBase, *pdstBase;    
  335.                 /* start of src and dst bitmaps */
  336.     int widthSrc, widthDst;    /* add to get to same position in next line */
  337.  
  338.     register BoxPtr pbox;
  339.     int nbox;
  340.  
  341.     BoxPtr pboxTmp, pboxNext, pboxBase, pboxNew1, pboxNew2;
  342.                 /* temporaries for shuffling rectangles */
  343.     DDXPointPtr pptTmp, pptNew1, pptNew2;
  344.                 /* shuffling boxes entails shuffling the
  345.                    source points too */
  346.     int w, h;
  347.     int xdir;            /* 1 = left right, -1 = right left/ */
  348.     int ydir;            /* 1 = top down, -1 = bottom up */
  349.  
  350.     unsigned int *psrcLine, *pdstLine;    
  351.                 /* pointers to line with current src and dst */
  352.     register unsigned int *psrc;/* pointer to current src longword */
  353.     register unsigned int *pdst;/* pointer to current dst longword */
  354.  
  355.                 /* following used for looping through a line */
  356.     unsigned int startmask, endmask;    /* masks for writing ends of dst */
  357.     int nlMiddle;        /* whole longwords in dst */
  358.     register int nl;        /* temp copy of nlMiddle */
  359.     register unsigned int tmpSrc;
  360.                 /* place to store full source word */
  361.     register int xoffSrc;    /* offset (>= 0, < 32) from which to
  362.                        fetch whole longwords fetched 
  363.                    in src */
  364.     int nstart;            /* number of ragged bits at start of dst */
  365.     int nend;            /* number of ragged bits at end of dst */
  366.     int srcStartOver;        /* pulling nstart bits from src
  367.                    overflows into the next word? */
  368.     int careful;
  369.  
  370.  
  371.     if (pSrcDrawable->type == DRAWABLE_WINDOW)
  372.     {
  373.     psrcBase = (unsigned int *)
  374.         (((PixmapPtr)(pSrcDrawable->pScreen->devPrivate))->devPrivate.ptr);
  375.     widthSrc = (int)
  376.            ((PixmapPtr)(pSrcDrawable->pScreen->devPrivate))->devKind
  377.             >> 2;
  378.     }
  379.     else
  380.     {
  381.     psrcBase = (unsigned int *)(((PixmapPtr)pSrcDrawable)->devPrivate.ptr);
  382.     widthSrc = (int)(((PixmapPtr)pSrcDrawable)->devKind) >> 2;
  383.     }
  384.  
  385.     if (pDstDrawable->type == DRAWABLE_WINDOW)
  386.     {
  387.     pdstBase = (unsigned int *)
  388.         (((PixmapPtr)(pDstDrawable->pScreen->devPrivate))->devPrivate.ptr);
  389.     widthDst = (int)
  390.            ((PixmapPtr)(pDstDrawable->pScreen->devPrivate))->devKind
  391.             >> 2;
  392.     }
  393.     else
  394.     {
  395.     pdstBase = (unsigned int *)(((PixmapPtr)pDstDrawable)->devPrivate.ptr);
  396.     widthDst = (int)(((PixmapPtr)pDstDrawable)->devKind) >> 2;
  397.     }
  398.  
  399.     /* XXX we have to err on the side of safety when both are windows,
  400.      * because we don't know if IncludeInferiors is being used.
  401.      */
  402.     careful = ((pSrcDrawable == pDstDrawable) ||
  403.            ((pSrcDrawable->type == DRAWABLE_WINDOW) &&
  404.         (pDstDrawable->type == DRAWABLE_WINDOW)));
  405.  
  406.     pbox = REGION_RECTS(prgnDst);
  407.     nbox = REGION_NUM_RECTS(prgnDst);
  408.  
  409.     pboxNew1 = NULL;
  410.     pptNew1 = NULL;
  411.     pboxNew2 = NULL;
  412.     pptNew2 = NULL;
  413.     if (careful && (pptSrc->y < pbox->y1))
  414.     {
  415.         /* walk source botttom to top */
  416.     ydir = -1;
  417.     widthSrc = -widthSrc;
  418.     widthDst = -widthDst;
  419.  
  420.     if (nbox > 1)
  421.     {
  422.         /* keep ordering in each band, reverse order of bands */
  423.         pboxNew1 = (BoxPtr)ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
  424.         if(!pboxNew1)
  425.         return;
  426.         pptNew1 = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * nbox);
  427.         if(!pptNew1)
  428.         {
  429.             DEALLOCATE_LOCAL(pboxNew1);
  430.             return;
  431.         }
  432.         pboxBase = pboxNext = pbox+nbox-1;
  433.         while (pboxBase >= pbox)
  434.         {
  435.             while ((pboxNext >= pbox) && 
  436.                (pboxBase->y1 == pboxNext->y1))
  437.             pboxNext--;
  438.             pboxTmp = pboxNext+1;
  439.             pptTmp = pptSrc + (pboxTmp - pbox);
  440.             while (pboxTmp <= pboxBase)
  441.             {
  442.             *pboxNew1++ = *pboxTmp++;
  443.             *pptNew1++ = *pptTmp++;
  444.             }
  445.             pboxBase = pboxNext;
  446.         }
  447.         pboxNew1 -= nbox;
  448.         pbox = pboxNew1;
  449.         pptNew1 -= nbox;
  450.         pptSrc = pptNew1;
  451.         }
  452.     }
  453.     else
  454.     {
  455.     /* walk source top to bottom */
  456.     ydir = 1;
  457.     }
  458.  
  459.     if (careful && (pptSrc->x < pbox->x1))
  460.     {
  461.     /* walk source right to left */
  462.         xdir = -1;
  463.  
  464.     if (nbox > 1)
  465.     {
  466.         /* reverse order of rects in each band */
  467.         pboxNew2 = (BoxPtr)ALLOCATE_LOCAL(sizeof(BoxRec) * nbox);
  468.         pptNew2 = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * nbox);
  469.         if(!pboxNew2 || !pptNew2)
  470.         {
  471.         if (pptNew2) DEALLOCATE_LOCAL(pptNew2);
  472.         if (pboxNew2) DEALLOCATE_LOCAL(pboxNew2);
  473.         if (pboxNew1)
  474.         {
  475.             DEALLOCATE_LOCAL(pptNew1);
  476.             DEALLOCATE_LOCAL(pboxNew1);
  477.         }
  478.             return;
  479.         }
  480.         pboxBase = pboxNext = pbox;
  481.         while (pboxBase < pbox+nbox)
  482.         {
  483.             while ((pboxNext < pbox+nbox) &&
  484.                (pboxNext->y1 == pboxBase->y1))
  485.             pboxNext++;
  486.             pboxTmp = pboxNext;
  487.             pptTmp = pptSrc + (pboxTmp - pbox);
  488.             while (pboxTmp != pboxBase)
  489.             {
  490.             *pboxNew2++ = *--pboxTmp;
  491.             *pptNew2++ = *--pptTmp;
  492.             }
  493.             pboxBase = pboxNext;
  494.         }
  495.         pboxNew2 -= nbox;
  496.         pbox = pboxNew2;
  497.         pptNew2 -= nbox;
  498.         pptSrc = pptNew2;
  499.     }
  500.     }
  501.     else
  502.     {
  503.     /* walk source left to right */
  504.         xdir = 1;
  505.     }
  506.  
  507.  
  508.     /* special case copy */
  509.     if (alu == GXcopy)
  510.     {
  511.     register unsigned int bits;
  512.     register unsigned int bits1;
  513.     int xoffSrc, xoffDst;
  514.     int    leftShift, rightShift;
  515.  
  516.     while(nbox--)
  517.     {
  518.         w = pbox->x2 - pbox->x1;
  519.         h = pbox->y2 - pbox->y1;
  520.  
  521.         if (ydir == -1) /* start at last scanline of rectangle */
  522.         {
  523.             psrcLine = psrcBase + ((pptSrc->y+h-1) * -widthSrc);
  524.             pdstLine = pdstBase + ((pbox->y2-1) * -widthDst);
  525.         }
  526.         else /* start at first scanline */
  527.         {
  528.             psrcLine = psrcBase + (pptSrc->y * widthSrc);
  529.             pdstLine = pdstBase + (pbox->y1 * widthDst);
  530.         }
  531.         if ((pbox->x1 & 0x1f) + w <= 32)
  532.         {
  533.         pdst = pdstLine + (pbox->x1 >> 5);
  534.         psrc = psrcLine + (pptSrc->x >> 5);
  535.         xoffSrc = pptSrc->x & 0x1f;
  536.         xoffDst = pbox->x1 & 0x1f;
  537.         while (h--)
  538.         {
  539.             getandputbits(psrc, xoffSrc, xoffDst, w, pdst);
  540.             psrc += widthSrc;
  541.             pdst += widthDst;
  542.         }
  543.         }
  544.         else
  545.         {
  546.             maskbits(pbox->x1, w, startmask, endmask, nlMiddle);
  547.             if (xdir == 1)
  548.             {
  549.                 xoffSrc = pptSrc->x & 0x1f;
  550.                 xoffDst = pbox->x1 & 0x1f;
  551.             pdstLine += (pbox->x1 >> 5);
  552.             psrcLine += (pptSrc->x >> 5);
  553.             if (xoffSrc == xoffDst)
  554.             {
  555.                     while (h--)
  556.                     {
  557.                     psrc = psrcLine;
  558.                     pdst = pdstLine;
  559.                     pdstLine += widthDst;
  560.                     psrcLine += widthSrc;
  561.                 if (startmask)
  562.                 {
  563.                     *pdst = (*pdst & ~startmask) | (*psrc++ & startmask);
  564.                     pdst++;
  565.                 }
  566.                 nl = nlMiddle;
  567.  
  568. #ifdef LARGE_INSTRUCTION_CACHE
  569. #ifdef FAST_CONSTANT_OFFSET_MODE
  570.  
  571.                 psrc += nl & (UNROLL-1);
  572.                 pdst += nl & (UNROLL-1);
  573.  
  574. #define BodyOdd(n) pdst[-n] = psrc[-n];
  575. #define BodyEven(n) pdst[-n] = psrc[-n];
  576.  
  577. #define LoopReset \
  578.     pdst += UNROLL; \
  579.     psrc += UNROLL;
  580.  
  581. #else
  582.  
  583. #define BodyOdd(n)  *pdst++ = *psrc++;
  584. #define BodyEven(n) BodyOdd(n)
  585.  
  586. #define LoopReset   ;
  587.  
  588. #endif
  589.                 PackedLoop
  590.  
  591. #undef BodyOdd
  592. #undef BodyEven
  593. #undef LoopReset
  594.  
  595. #else
  596.                 DuffL(nl, label1, *pdst++ = *psrc++;)
  597. #endif
  598.                 if (endmask)
  599.                     *pdst = (*pdst & ~endmask) | (*psrc++ & endmask);
  600.                 }
  601.             }
  602.             else
  603.             {
  604.                 if (xoffSrc > xoffDst)
  605.             {
  606.                 leftShift = (xoffSrc - xoffDst);
  607.                 rightShift = 32 - leftShift;
  608.             }
  609.                 else
  610.             {
  611.                 rightShift = (xoffDst - xoffSrc);
  612.                 leftShift = 32 - rightShift;
  613.             }
  614.                 while (h--)
  615.                 {
  616.                 psrc = psrcLine;
  617.                 pdst = pdstLine;
  618.                 pdstLine += widthDst;
  619.                 psrcLine += widthSrc;
  620.                 bits = 0;
  621.                 if (xoffSrc > xoffDst)
  622.                     bits = *psrc++;
  623.                 if (startmask)
  624.                 {
  625.                     bits1 = SCRLEFT(bits,leftShift);
  626.                     bits = *psrc++;
  627.                     bits1 |= SCRRIGHT(bits,rightShift);
  628.                     *pdst = (*pdst & ~startmask) |
  629.                         (bits1 & startmask);
  630.                     pdst++;
  631.                 }
  632.                 nl = nlMiddle;
  633.  
  634. #ifdef LARGE_INSTRUCTION_CACHE
  635.                 bits1 = bits;
  636. #ifdef FAST_CONSTANT_OFFSET_MODE
  637.  
  638.                 psrc += nl & (UNROLL-1);
  639.                 pdst += nl & (UNROLL-1);
  640.  
  641. #define BodyOdd(n) \
  642.     bits = psrc[-n]; \
  643.     pdst[-n] = BitLeft(bits1, leftShift) | BitRight(bits, rightShift);
  644.  
  645. #define BodyEven(n) \
  646.     bits1 = psrc[-n]; \
  647.     pdst[-n] = BitLeft(bits, leftShift) | BitRight(bits1, rightShift);
  648.  
  649. #define LoopReset \
  650.     pdst += UNROLL; \
  651.     psrc += UNROLL;
  652.  
  653. #else
  654.  
  655. #define BodyOdd(n) \
  656.     bits = *psrc++; \
  657.     *pdst++ = BitLeft(bits1, leftShift) | BitRight(bits, rightShift);
  658.                
  659. #define BodyEven(n) \
  660.     bits1 = *psrc++; \
  661.     *pdst++ = BitLeft(bits, leftShift) | BitRight(bits1, rightShift);
  662.  
  663. #define LoopReset   ;
  664.  
  665. #endif    /* !FAST_CONSTANT_OFFSET_MODE */
  666.  
  667.                 PackedLoop
  668.  
  669. #undef BodyOdd
  670. #undef BodyEven
  671. #undef LoopReset
  672.     
  673. #else
  674.                 DuffL (nl,label2,
  675.                 bits1 = BitLeft(bits, leftShift);
  676.                 bits = *psrc++;
  677.                 *pdst++ = bits1 | BitRight(bits, rightShift);
  678.                 )
  679. #endif
  680.                 if (endmask)
  681.                 {
  682.                     bits1 = SCRLEFT(bits, leftShift);
  683.                     if (SCRLEFT(endmask, rightShift))
  684.                     {
  685.                     bits = *psrc++;
  686.                     bits1 |= SCRRIGHT(bits, rightShift);
  687.                     }
  688.                     *pdst = (*pdst & ~endmask) |
  689.                         (bits1 & endmask);
  690.                 }
  691.                 }
  692.             }
  693.             }
  694.             else    /* xdir == -1 */
  695.             {
  696.                 xoffSrc = (pptSrc->x + w - 1) & 0x1f;
  697.                 xoffDst = (pbox->x2 - 1) & 0x1f;
  698.             pdstLine += ((pbox->x2-1) >> 5) + 1;
  699.             psrcLine += ((pptSrc->x+w - 1) >> 5) + 1;
  700.             if (xoffSrc == xoffDst)
  701.             {
  702.                     while (h--)
  703.                     {
  704.                     psrc = psrcLine;
  705.                     pdst = pdstLine;
  706.                     pdstLine += widthDst;
  707.                     psrcLine += widthSrc;
  708.                 if (endmask)
  709.                 {
  710.                     pdst--;
  711.                     *pdst = (*pdst & ~endmask) | (*--psrc & endmask);
  712.                 }
  713.                 nl = nlMiddle;
  714.  
  715. #ifdef LARGE_INSTRUCTION_CACHE
  716. #ifdef FAST_CONSTANT_OFFSET_MODE
  717.                 psrc -= nl & (UNROLL - 1);
  718.                 pdst -= nl & (UNROLL - 1);
  719.  
  720. #define BodyOdd(n) pdst[n-1] = psrc[n-1];
  721.  
  722. #define BodyEven(n) pdst[n-1] = psrc[n-1];
  723.  
  724. #define LoopReset \
  725.     pdst -= UNROLL;\
  726.     psrc -= UNROLL;
  727.  
  728. #else
  729.  
  730. #define BodyOdd(n)  *--pdst = *--psrc;
  731. #define BodyEven(n) BodyOdd(n)
  732. #define LoopReset   ;
  733.  
  734. #endif
  735.                 PackedLoop
  736.  
  737. #undef BodyOdd
  738. #undef BodyEven
  739. #undef LoopReset
  740.  
  741. #else
  742.                 DuffL(nl,label3, *--pdst = *--psrc;)
  743. #endif
  744.                 if (startmask)
  745.                 {
  746.                     --pdst;
  747.                     *pdst = (*pdst & ~startmask) | (*--psrc & startmask);
  748.                 }
  749.                 }
  750.             }
  751.             else
  752.             {
  753.             if (xoffDst > xoffSrc)
  754.             {
  755.                 rightShift = (xoffDst - xoffSrc);
  756.                 leftShift = 32 - rightShift;
  757.             }
  758.             else
  759.             {
  760.                     leftShift = (xoffSrc - xoffDst);
  761.                     rightShift = 32 - leftShift;
  762.             }
  763.                     while (h--)
  764.                     {
  765.                     psrc = psrcLine;
  766.                     pdst = pdstLine;
  767.                     pdstLine += widthDst;
  768.                     psrcLine += widthSrc;
  769.                 bits = 0;
  770.                 if (xoffDst > xoffSrc)
  771.                 bits = *--psrc;
  772.                 if (endmask)
  773.                 {
  774.                     bits1 = SCRRIGHT(bits, rightShift);
  775.                     bits = *--psrc;
  776.                     bits1 |= SCRLEFT(bits, leftShift);
  777.                     pdst--;
  778.                     *pdst = (*pdst & ~endmask) |
  779.                         (bits1 & endmask);
  780.                 }
  781.                 nl = nlMiddle;
  782.  
  783. #ifdef LARGE_INSTRUCTION_CACHE
  784.                 bits1 = bits;
  785. #ifdef FAST_CONSTANT_OFFSET_MODE
  786.                 psrc -= nl & (UNROLL - 1);
  787.                 pdst -= nl & (UNROLL - 1);
  788.  
  789. #define BodyOdd(n) \
  790.     bits = psrc[n-1]; \
  791.     pdst[n-1] = BitRight(bits1, rightShift) | BitLeft(bits, leftShift);
  792.  
  793. #define BodyEven(n) \
  794.     bits1 = psrc[n-1]; \
  795.     pdst[n-1] = BitRight(bits, rightShift) | BitLeft(bits1, leftShift);
  796.  
  797. #define LoopReset \
  798.     pdst -= UNROLL; \
  799.     psrc -= UNROLL;
  800.  
  801. #else
  802.  
  803. #define BodyOdd(n) \
  804.     bits = *--psrc; \
  805.     *--pdst = BitRight(bits1, rightShift) | BitLeft(bits, leftShift);
  806.  
  807. #define BodyEven(n) \
  808.     bits1 = *--psrc; \
  809.     *--pdst = BitRight(bits, rightShift) | BitLeft(bits1, leftShift);
  810.  
  811. #define LoopReset   ;
  812.  
  813. #endif
  814.  
  815.                 PackedLoop
  816.  
  817. #undef BodyOdd
  818. #undef BodyEven
  819. #undef LoopReset
  820.  
  821. #else
  822.                 DuffL (nl, label4,
  823.                 bits1 = BitRight(bits, rightShift);
  824.                 bits = *--psrc;
  825.                 *--pdst = bits1 | BitLeft(bits, leftShift);
  826.                 )
  827. #endif
  828.  
  829.                 if (startmask)
  830.                 {
  831.                     bits1 = SCRRIGHT(bits, rightShift);
  832.                     if (SCRRIGHT (startmask, leftShift))
  833.                     {
  834.                     bits = *--psrc;
  835.                     bits1 |= SCRLEFT(bits, leftShift);
  836.                     }
  837.                     --pdst;
  838.                     *pdst = (*pdst & ~startmask) |
  839.                         (bits1 & startmask);
  840.                 }
  841.                 }
  842.             }
  843.             }
  844.         }
  845.         pbox++;
  846.         pptSrc++;
  847.     }
  848.     }
  849.     else /* do some rop */
  850.     {
  851.         while (nbox--)
  852.         {
  853.         w = pbox->x2 - pbox->x1;
  854.         h = pbox->y2 - pbox->y1;
  855.  
  856.         if (ydir == -1) /* start at last scanline of rectangle */
  857.         {
  858.             psrcLine = psrcBase + ((pptSrc->y+h-1) * -widthSrc);
  859.             pdstLine = pdstBase + ((pbox->y2-1) * -widthDst);
  860.         }
  861.         else /* start at first scanline */
  862.         {
  863.             psrcLine = psrcBase + (pptSrc->y * widthSrc);
  864.             pdstLine = pdstBase + (pbox->y1 * widthDst);
  865.         }
  866.  
  867.         /* x direction doesn't matter for < 1 longword */
  868.         if (w <= 32)
  869.         {
  870.             int srcBit, dstBit;    /* bit offset of src and dst */
  871.  
  872.             pdstLine += (pbox->x1 >> 5);
  873.             psrcLine += (pptSrc->x >> 5);
  874.             psrc = psrcLine;
  875.             pdst = pdstLine;
  876.  
  877.             srcBit = pptSrc->x & 0x1f;
  878.             dstBit = pbox->x1 & 0x1f;
  879.  
  880.             while(h--)
  881.             {
  882.             getandputrop(psrc, srcBit, dstBit, w, pdst, alu)
  883.             pdst += widthDst;
  884.             psrc += widthSrc;
  885.             }
  886.         }
  887.         else
  888.         {
  889.             maskbits(pbox->x1, w, startmask, endmask, nlMiddle)
  890.             if (startmask)
  891.             nstart = 32 - (pbox->x1 & 0x1f);
  892.             else
  893.             nstart = 0;
  894.             if (endmask)
  895.                 nend = pbox->x2 & 0x1f;
  896.             else
  897.             nend = 0;
  898.  
  899.             xoffSrc = ((pptSrc->x & 0x1f) + nstart) & 0x1f;
  900.             srcStartOver = ((pptSrc->x & 0x1f) + nstart) > 31;
  901.  
  902.             if (xdir == 1) /* move left to right */
  903.             {
  904.                 pdstLine += (pbox->x1 >> 5);
  905.                 psrcLine += (pptSrc->x >> 5);
  906.  
  907.             while (h--)
  908.             {
  909.                 psrc = psrcLine;
  910.                 pdst = pdstLine;
  911.  
  912.                 if (startmask)
  913.                 {
  914.                 getandputrop(psrc, (pptSrc->x & 0x1f), 
  915.                      (pbox->x1 & 0x1f), nstart, pdst, alu)
  916.                 pdst++;
  917.                 if (srcStartOver)
  918.                     psrc++;
  919.                 }
  920.  
  921.             /* special case for aligned operations */
  922.             if (xoffSrc == 0)
  923.             {
  924.                 nl = nlMiddle;
  925.                 while (nl--)
  926.                 {
  927.                 DoRop (*pdst, alu, *psrc++, *pdst);
  928.                  pdst++;
  929.                 }
  930.             }
  931.              else
  932.             {
  933.                 nl = nlMiddle + 1;
  934.                 while (--nl)
  935.                     {
  936.                 getunalignedword (psrc, xoffSrc, tmpSrc)
  937.                 DoRop (*pdst, alu, tmpSrc, *pdst);
  938.                 pdst++;
  939.                 psrc++;
  940.                 }
  941.             }
  942.  
  943.                 if (endmask)
  944.                 {
  945.                 getandputrop0(psrc, xoffSrc, nend, pdst, alu);
  946.                 }
  947.  
  948.                 pdstLine += widthDst;
  949.                 psrcLine += widthSrc;
  950.             }
  951.             }
  952.             else /* move right to left */
  953.             {
  954.                 pdstLine += (pbox->x2 >> 5);
  955.                 psrcLine += (pptSrc->x+w >> 5);
  956.             /* if fetch of last partial bits from source crosses
  957.                a longword boundary, start at the previous longword
  958.             */
  959.             if (xoffSrc + nend >= 32)
  960.                 --psrcLine;
  961.  
  962.             while (h--)
  963.             {
  964.                 psrc = psrcLine;
  965.                 pdst = pdstLine;
  966.  
  967.                 if (endmask)
  968.                 {
  969.                 getandputrop0(psrc, xoffSrc, nend, pdst, alu);
  970.                 }
  971.  
  972.                 nl = nlMiddle + 1;
  973.                 while (--nl)
  974.                 {
  975.                 --psrc;
  976.                 --pdst;
  977.                 getunalignedword(psrc, xoffSrc, tmpSrc)
  978.                 DoRop(*pdst, alu, tmpSrc, *pdst);
  979.                 }
  980.  
  981.                 if (startmask)
  982.                 {
  983.                 if (srcStartOver)
  984.                     --psrc;
  985.                 --pdst;
  986.                 getandputrop(psrc, (pptSrc->x & 0x1f), 
  987.                      (pbox->x1 & 0x1f), nstart, pdst, alu)
  988.                 }
  989.  
  990.                 pdstLine += widthDst;
  991.                 psrcLine += widthSrc;
  992.             }
  993.             } /* move right to left */
  994.         }
  995.         pbox++;
  996.         pptSrc++;
  997.         } /* while (nbox--) */
  998.     }
  999.  
  1000.     /* free up stuff */
  1001.     if (pboxNew2)
  1002.     {
  1003.     DEALLOCATE_LOCAL(pptNew2);
  1004.     DEALLOCATE_LOCAL(pboxNew2);
  1005.     }
  1006.     if (pboxNew1)
  1007.     {
  1008.     DEALLOCATE_LOCAL(pptNew1);
  1009.     DEALLOCATE_LOCAL(pboxNew1);
  1010.     }
  1011. }
  1012.  
  1013. /*
  1014.  * Allow devices which use mfb for 1-bit pixmap support
  1015.  * to register a function for n-to-1 copy operations, instead
  1016.  * of falling back to miCopyPlane
  1017.  */
  1018.  
  1019. static unsigned long    copyPlaneGeneration;
  1020. static int        copyPlaneScreenIndex = -1;
  1021.  
  1022. Bool
  1023. mfbRegisterCopyPlaneProc (pScreen, proc)
  1024.     ScreenPtr    pScreen;
  1025.     RegionPtr    (*proc)();
  1026. {
  1027.     if (copyPlaneGeneration != serverGeneration)
  1028.     {
  1029.     copyPlaneScreenIndex = AllocateScreenPrivateIndex();
  1030.     if (copyPlaneScreenIndex < 0)
  1031.         return FALSE;
  1032.     copyPlaneGeneration = serverGeneration;
  1033.     }
  1034.     pScreen->devPrivates[copyPlaneScreenIndex].ptr = (pointer) proc;
  1035.     return TRUE;
  1036. }
  1037.  
  1038. /*
  1039.     if fg == 1 and bg ==0, we can do an ordinary CopyArea.
  1040.     if fg == bg, we can do a CopyArea with alu = mfbReduceRop(alu, fg)
  1041.     if fg == 0 and bg == 1, we use the same rasterop, with
  1042.     source operand inverted.
  1043.  
  1044.     CopyArea deals with all of the graphics exposure events.
  1045.     This code depends on knowing that we can change the
  1046. alu in the GC without having to call ValidateGC() before calling
  1047. CopyArea().
  1048.  
  1049. */
  1050.  
  1051. RegionPtr
  1052. mfbCopyPlane(pSrcDrawable, pDstDrawable,
  1053.         pGC, srcx, srcy, width, height, dstx, dsty, plane)
  1054. DrawablePtr pSrcDrawable, pDstDrawable;
  1055. register GC *pGC;
  1056. int srcx, srcy;
  1057. int width, height;
  1058. int dstx, dsty;
  1059. unsigned long plane;
  1060. {
  1061.     int alu;
  1062.     RegionPtr    prgnExposed;
  1063.     RegionPtr    (*copyPlane)();
  1064.  
  1065.     if (pSrcDrawable->depth != 1)
  1066.     {
  1067.     if (copyPlaneScreenIndex >= 0 &&
  1068.         (copyPlane = (RegionPtr (*)()) 
  1069.         pSrcDrawable->pScreen->devPrivates[copyPlaneScreenIndex].ptr)
  1070.         )
  1071.     {
  1072.         return (*copyPlane) (pSrcDrawable, pDstDrawable,
  1073.                pGC, srcx, srcy, width, height, dstx, dsty, plane);
  1074.     }
  1075.     else
  1076.         return miCopyPlane(pSrcDrawable, pDstDrawable,
  1077.                pGC, srcx, srcy, width, height, dstx, dsty, plane);
  1078.     }
  1079.     if (plane != 1)
  1080.     return NULL;
  1081.  
  1082.     if ((pGC->fgPixel == 1) && (pGC->bgPixel == 0))
  1083.     {
  1084.     prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
  1085.              pGC, srcx, srcy, width, height, dstx, dsty);
  1086.     }
  1087.     else if (pGC->fgPixel == pGC->bgPixel)
  1088.     {
  1089.     alu = pGC->alu;
  1090.     pGC->alu = mfbReduceRop(pGC->alu, pGC->fgPixel);
  1091.     prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
  1092.              pGC, srcx, srcy, width, height, dstx, dsty);
  1093.     pGC->alu = alu;
  1094.     }
  1095.     else /* need to invert the src */
  1096.     {
  1097.     alu = pGC->alu;
  1098.     pGC->alu = InverseAlu[alu];
  1099.     prgnExposed = (*pGC->ops->CopyArea)(pSrcDrawable, pDstDrawable,
  1100.              pGC, srcx, srcy, width, height, dstx, dsty);
  1101.     pGC->alu = alu;
  1102.     }
  1103.     return prgnExposed;
  1104. }
  1105.